package com.christina.engine.parameter.builder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 参数建造者工厂
 * 提供统一的参数建造者创建接口
 * 
 * @author Christina
 */
@Component
public class ParameterBuilderFactory {

    private ApplicationContext applicationContext;

    @Autowired
    public ParameterBuilderFactory(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * 创建出行规划请求建造者
     * 
     * @return TravelPlanRequestBuilder实例
     */
    public TravelPlanRequestBuilder createTravelPlanRequestBuilder() {
        return TravelPlanRequestBuilder.newBuilder();
    }
    
    /**
     * 创建聊天请求建造者
     * 
     * @return ChatRequestBuilder实例
     */
    public ChatRequestBuilder createChatRequestBuilder() {
        return ChatRequestBuilder.newBuilder();
    }
    
    /**
     * 创建用户偏好建造者
     * 
     * @return UserPreferencesBuilder实例
     */
    public UserPreferencesBuilder createUserPreferencesBuilder() {
        return UserPreferencesBuilder.newBuilder();
    }
    
    /**
     * 根据服务类型创建对应的建造者
     * 
     * @param serviceType 服务类型
     * @return 对应的建造者实例
     * @throws IllegalArgumentException 如果服务类型不支持
     */
    public ParameterBuilder<?> createBuilder(String serviceType) {
        return switch (serviceType.toLowerCase()) {
            case "travel" -> createTravelPlanRequestBuilder();
            case "chat" -> createChatRequestBuilder();
            case "userpreferences", "preferences" -> createUserPreferencesBuilder();
            default -> throw new IllegalArgumentException("不支持的服务类型: " + serviceType);
        };
    }
    
    /**
     * 检查是否支持指定的服务类型
     * 
     * @param serviceType 服务类型
     * @return 是否支持
     */
    public boolean supports(String serviceType) {
        if (serviceType == null) {
            return false;
        }
        
        return switch (serviceType.toLowerCase()) {
            case "travel", "chat", "userpreferences", "preferences" -> true;
            default -> false;
        };
    }
    
    /**
     * 检查是否支持指定的类型
     * 
     * @param type 类型
     * @return 是否支持
     */
    public boolean supportsType(Class<?> type) {
        if (type == null) {
            return false;
        }
        
        String typeName = type.getSimpleName().toLowerCase();
        return supports(typeName) || 
               typeName.contains("travel") || 
               typeName.contains("chat") || 
               typeName.contains("user") ||
               typeName.contains("preference");
    }
    
    /**
     * 获取所有支持的服务类型
     * 
     * @return 支持的服务类型数组
     */
    public String[] getSupportedServiceTypes() {
        return new String[]{"travel", "chat", "userpreferences"};
    }
}